#include <iostream>
#include <fstream>
#include "windows.h"
#include "constants.h"
#include "general.h"
#include "trace.h"
#include "csma_ca.h"
#include "csma.h"
#include "ackcon.h"
#include "t2f.h"

using namespace std;

void testTrace(int nodeNumber)
{
	for (int i = 1; i <= 6; i++)
	{
		Sleep(1000);
		Trace trace;
		char nodefile[256];
		sprintf_s(nodefile, 256, "traces/nodes_%d_%d.txt", nodeNumber, i);
		if (trace.generateNodes(nodeNumber, nodefile))
			cout << "generate nodes successful";
		else
			cout << "fail in generate nodes" << endl;

		char tracefile[256];
		sprintf_s(tracefile, 256, "traces/traffic_%d_%d.txt", nodeNumber, i);
		trace.generateTraffic(100, 10, 3, tracefile);
	}
}

void testCSMA(int datarate = 6)
{
	//Trace trace;
	//trace.generateNodesFromFile("nodes_3.txt");
	//trace.generateTrafficFromFile("traffic_4.txt");
	//
	//CSMA csmaTester;
	//csmaTester.initial(trace, datarate);
	//csmaTester.start_mac();

	char outputfile[256];
	sprintf_s(outputfile, 256, "results/csma_%d.txt", datarate);

	ofstream fout(outputfile);

	for (int i = 10; i < 70; i+=10)
	{
		for (int j = 1; j <= 6; j++)
		{
			Trace trace;
			char nodefile[256];
			sprintf_s(nodefile, 256, "traces/nodes_%d_%d.txt", i, j);

			char trafficfile[256];
			sprintf_s(trafficfile, 256, "traces/traffic_%d_%d.txt", i, j);

			trace.generateNodesFromFile(nodefile);
			trace.generateTrafficFromFile(trafficfile);

			CSMA csmaTester;
			csmaTester.initial(trace, datarate);
			csmaTester.start_mac();

			fout << i << "\t" << j << "\t";
			fout << csmaTester.time << "\t" << csmaTester.collisionCount << endl;
		}
	}
}

void testCSMACA(int datarate = 6)
{
	//Trace trace;
	//trace.generateNodesFromFile("nodes_3.txt");
	//trace.generateTrafficFromFile("traffic_4.txt");
	//
	//CSMA_CA csmacaTester;
	//csmacaTester.initial(trace, datarate);
	//csmacaTester.start_mac();


	char outputfile[256];
	sprintf_s(outputfile, 256, "results/csmaca_%d.txt", datarate);

	ofstream fout(outputfile);

	for (int i = 10; i < 70; i+=10)
	{
		for (int j = 1; j <= 6; j++)
		{
			Trace trace;
			char nodefile[256];
			sprintf_s(nodefile, 256, "traces/nodes_%d_%d.txt", i, j);

			char trafficfile[256];
			sprintf_s(trafficfile, 256, "traces/traffic_%d_%d.txt", i, j);

			trace.generateNodesFromFile(nodefile);
			trace.generateTrafficFromFile(trafficfile);

			CSMA_CA csmacaTester;
			csmacaTester.initial(trace, datarate);
			csmacaTester.start_mac();

			fout << i << "\t" << j << "\t";
			fout << csmacaTester.time << "\t" << csmacaTester.collisionCount << endl;
		}
	}
}

void testACK(int datarate = 6)
{
	//Trace trace;
	//trace.generateNodesFromFile("nodes_30.txt");
	//trace.generateTrafficFromFile("traffic_30.txt");
	//
	//ACKcon ackconTester;
	//ackconTester.initial(trace, datarate);
	//ackconTester.start_mac();

	char outputfile[256];
	sprintf_s(outputfile, 256, "results/repick_%d.txt", datarate);

	ofstream fout(outputfile);

	for (int i = 10; i < 70; i+=10)
	{
		for (int j = 1; j <= 6; j++)
		{
			Trace trace;
			char nodefile[256];
			sprintf_s(nodefile, 256, "traces/nodes_%d_%d.txt", i, j);

			char trafficfile[256];
			sprintf_s(trafficfile, 256, "traces/traffic_%d_%d.txt", i, j);

			trace.generateNodesFromFile(nodefile);
			trace.generateTrafficFromFile(trafficfile);

			ACKcon ackconTester;
			ackconTester.initial(trace, datarate);
			ackconTester.start_mac();

			fout << i << "\t" << j << "\t";
			fout << ackconTester.time << "\t" << ackconTester.collisionCount << endl;
		}
	}
}

void testT2F(int datarate = 6)
{
	//Trace trace;
	//trace.generateNodesFromFile("nodes_30.txt");
	//trace.generateTrafficFromFile("traffic_30.txt");
	//
	//T2F t2fTester;
	//t2fTester.initial(trace, datarate);
	//t2fTester.start_mac();

	char outputfile[256];
	sprintf_s(outputfile, 256, "results/t2f_%d.txt", datarate);

	ofstream fout(outputfile);

	for (int i = 10; i < 70; i+=10)
	{
		for (int j = 1; j <= 6; j++)
		{
			Trace trace;
			char nodefile[256];
			sprintf_s(nodefile, 256, "traces/nodes_%d_%d.txt", i, j);

			char trafficfile[256];
			sprintf_s(trafficfile, 256, "traces/traffic_%d_%d.txt", i, j);

			trace.generateNodesFromFile(nodefile);
			trace.generateTrafficFromFile(trafficfile);

			T2F t2fTester;
			t2fTester.initial(trace, datarate);
			t2fTester.start_mac();

			fout << i << "\t" << j << "\t";
			fout << t2fTester.time << "\t" << t2fTester.collisionCount << endl;
		}
	}
}

int main()
{
	int datarates[4] = {6, 18, 36, 54};
	//for (int i = 10; i < 70; i+=10)
	//{
	//	testTrace(i);
	//}
	for (int i = 0; i < 1; i++)
	{
		int datarate = datarates[i];
		testCSMA(datarate);
	}
	//testACK(datarate);
	//testT2F(datarate);
	//testCSMACA(datarate);
	return 0;
}